package com.micl.fileserver.controller.v2;


import com.micl.fileserver.tools.JsonMessage;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

import com.micl.fileserver.tools.FileSystemTools;

/**
 * @author yanghaoyu
 * 2.0文件上传api 舍弃mongoDB采用纯文件系统方式
 */
@RestController(value = "fileController_v2")
public class FileController {

    @Value(value = "${micl.upload.file.path.temp}")
    private String decryptFilePath;
    @Value(value = "${micl.upload.file.path}")
    private String mergeFilePath;
    @Autowired
    private FileSystemTools fileSystemTools;


    @GetMapping(value = "/uuid", produces = "application/json;charset=UTF-8")
    public String beforeUpload() {
        String uuid = fileSystemTools.makeDirWithUUID();
        JsonMessage jsonMessage = new JsonMessage(0, "uuid创建成功");
        jsonMessage.add("uuid", uuid);
        return jsonMessage.toString();
    }

    @GetMapping(value = "/slice", produces = "application/json;charset=UTF-8")
    public String sliceCheck(@RequestParam(name = "number") String number,
                             @RequestParam(name = "uuid") String uuid) {

        JsonMessage jsonMessage = new JsonMessage();
        if (fileSystemTools.checkPartExist(uuid, number)) {
            jsonMessage.add(1, "分片已存在");
            return jsonMessage.toString();
        } else {
            jsonMessage.add(0, "分片不存在");
            return jsonMessage.toString();
        }

    }

    @PostMapping(value = "/slice", produces = "application/json;charset=UTF-8")
    public String sliceUpload(HttpServletRequest request,
                              @RequestParam(name = "file") MultipartFile file,
                              @RequestParam(name = "number") String number, //number 务必从0开始算起
                              @RequestParam(name = "uuid") String uuid) throws IOException {
        JsonMessage jsonMessage = new JsonMessage();
        if (!ServletFileUpload.isMultipartContent(request)) {
            jsonMessage.add(1, "上传格式错误，不是MutipartContent");
            return jsonMessage.toString();
        }
        try {
            fileSystemTools.storeFileByStream(file.getInputStream(), uuid, number);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        jsonMessage.add(0, "分片上传成功");
        return jsonMessage.toString();
    }

    @PostMapping(value = "/bigfile", produces = "application/json;charset=UTF-8")
    public String afterUpload(@RequestParam(name = "uuid") String uuid,
                              @RequestParam(name = "count") int count,
                              @RequestParam(name = "fileName") String fileName) throws IOException {

        JsonMessage jsonMessage = new JsonMessage();
        if (!fileSystemTools.checkPartCounts(uuid, count)) {
            jsonMessage.add(1, "uuid错误或count分片总数不吻合");
            return jsonMessage.toString();
        }
        try {
            File mergeFile = fileSystemTools.mergeFileByUUID(uuid, fileName, count);
            jsonMessage.add(0, "merge完成");
            jsonMessage.add("path", mergeFile.getAbsolutePath());
            return jsonMessage.toString();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
    }

    @GetMapping(value = "/bigfile/info", produces = "application/json;charset=UTF-8")
    public String getFileInfo(@RequestParam(name = "path") String path) {
        if (fileSystemTools.checkFileExist(path)) {
            JsonMessage jsonMessage = new JsonMessage(0, "文件存在");
            return jsonMessage.toString();
        } else {
            JsonMessage jsonMessage = new JsonMessage(1, "文件不存在");
            return jsonMessage.toString();
        }
    }

}
